/*
* Copyright (c) 2024 -present FengJinjian ( 751965763@qq.com )
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ${package.ServiceImpl};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.vital.energy.longevity.domain.dto.${entity?lower_case}.${entity}CreateDTO;
import edu.vital.energy.longevity.domain.dto.${entity?lower_case}.${entity}QueryDTO;
import edu.vital.energy.longevity.domain.dto.${entity?lower_case}.${entity}UpdateDTO;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* ${table.comment!}服务实现类
*
* @version 1.0.1
* @author ${author}
* @date ${date}
*/
@Slf4j
@Service
<#if kotlin>
 open class ${table.serviceImplName} : ${table.serviceName} {
<#else>
 public class ${table.serviceImplName} implements ${table.serviceName} {

 @Autowired
 private ${table.mapperName} ${table.entityPath}Mapper;

 @Override
 public Map<String, Object> get${entity}Page(Integer pageNum, Integer pageSize, ${entity}QueryDTO query) {
 // 构建查询条件
 LambdaQueryWrapper<${entity}> wrapper = new LambdaQueryWrapper<>();

 // TODO: 根据实际字段添加查询条件
 if (StringUtils.hasText(query.getName())) {
 wrapper.like(${entity}::getName, query.getName());
 }
 if (query.getStatus() != null) {
 wrapper.eq(${entity}::getStatus, query.getStatus());
 }

 // 按更新时间倒序
 wrapper.orderByDesc(${entity}::getUpdateTime);

 // 执行分页查询
 IPage<${entity}> page = ${table.entityPath}Mapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

 // 包装返回结果
 Map<String, Object> result = new HashMap<>();
 result.put("records", page.getRecords());
 result.put("current", page.getCurrent());
 result.put("size", page.getSize());
 result.put("total", page.getTotal());
 result.put("pages", page.getPages());

 return result;
 }

 @Override
 public ${entity} get${entity}ById(Long id) {
 ${entity} ${table.entityPath} = ${table.entityPath}Mapper.selectById(id);
 if (${table.entityPath} == null) {
 throw new IllegalArgumentException("${table.comment!}不存在");
 }
 return ${table.entityPath};
 }

 @Override
 @Transactional(rollbackFor = Exception.class)
 public Long create${entity}(${entity}CreateDTO dto) {
 ${entity} ${table.entityPath} = new ${entity}();
 BeanUtils.copyProperties(dto, ${table.entityPath});
 ${table.entityPath}.setStatus(1); // 设置初始状态

 ${table.entityPath}Mapper.insert(${table.entityPath});
 log.info("创建${table.comment!}成功: id={}", ${table.entityPath}.getId());

 return ${table.entityPath}.getId();
 }

 @Override
 @Transactional(rollbackFor = Exception.class)
 public int update${entity}(Long id, ${entity}UpdateDTO dto) {
 ${entity} ${table.entityPath} = get${entity}ById(id);
 BeanUtils.copyProperties(dto, ${table.entityPath});

 int rows = ${table.entityPath}Mapper.updateById(${table.entityPath});
 log.info("更新${table.comment!}成功: id={}, 影响行数={}", id, rows);
 return rows;
 }

 @Override
 @Transactional(rollbackFor = Exception.class)
 public int delete${entity}(Long id) {
 int rows = ${table.entityPath}Mapper.deleteById(id);
 if (rows <= 0) {
 throw new IllegalArgumentException("${table.comment!}不存在");
 }
 log.info("删除${table.comment!}成功: id={}, 影响行数={}", id, rows);
 return rows;
 }

 @Override
 @Transactional(rollbackFor = Exception.class)
 public int batchDelete${entity}(List<Long> ids) {
 int rows = ${table.entityPath}Mapper.deleteBatchIds(ids);
 log.info("批量删除${table.comment!}成功: ids={}, 影响行数={}", ids, rows);
 return rows;
 }




 @Override
 @Transactional(rollbackFor = Exception.class)
 public void update${entity}Status(Long id, Integer status) {
 ${entity} ${table.entityPath} = get${entity}ById(id);

 ${table.entityPath}Mapper.updateById(${table.entityPath});
 log.info("更新${table.comment!}状态成功: id={}, status={}", id, status);
 }
 }
</#if>